var shoubound = false;
$(document).ready(function(){
	function tm1tips() {
		var offset = $("#tm1").offset();
		$("#tm1tips").css({ left:offset.left+80, top:offset.top+3});
		$("#tm1tips").show();
	}
	function tm2tips() {
		var offset = $("#tm2").offset();
		$("#tm2tips").css({ left:offset.left+80, top:offset.top+3});
		$("#tm2tips").show();
	}
	tm1tips();tm2tips();
	$("#roomnum").change(function(){
		var rule = $("#rule").val();
		rules = eval("("+rule+")");
		var hid = $("#hid").val();
		var rid = $("#rid").val();
		var pid = $("#pid").val();
		var tm1 = $("#tm1").val();
		var tm2 = $("#tm2").val();
		var deposit = $("#deposit").val();
		var roomnum = $("#roomnum").val();
		var allfullfees = $("#allfullfees").val();
		var fullfees = $("#fullfees").val();
		var firstNightFees = $("#firstNightFees").val();
		var arrvialearlyElement = document.getElementById('arrvialearly');
		if (!arrvialearlyElement.checked) arrvialearlyElement.checked = true;
		var arriveTime = $("input[name='arriveTime']:checked").val();
		var arrtime = arriveTime.split(':');
		if(rules.length >0) {  //担保信息是否为空
		
			var garantext = '';
			var bookingDate = new Date();
			var sArray = tm1.split('-');
			var checkInDate = new Date(parseInt(sArray[0],10),(parseInt(sArray[1],10)-1),parseInt(sArray[2],10));
			var arriveTime2 = new Date(2011, 5, 20, parseInt(arrtime[0],10), parseInt(arrtime[1],10), 0);
			// 房间数量
			var roomCount = roomnum;
			var fullyFees = fullfees;

			var result = checkGaranteeableByArray(hotelGaranteeRule, bookingDate, checkInDate, arriveTime2, parseInt(roomCount), parseInt(firstNightFees), parseInt(fullyFees));
			setprice(roomnum,fullfees);
			if (result) {
				garantext = garanteeableMessage;
				setgarantee(garantext,deposit);
			} else {
				$("#ischeckguarantee").val('no');
				$("#checkguarantee").hide();	
			}
		} else {
			setprice(roomnum,fullfees);
		}
	});
	$("#arrviallate,#arrvialearly").click(function(){
		var rule = $("#rule").val();
		rules = eval("("+rule+")");
		var hid = $("#hid").val();
		var rid = $("#rid").val();
		var pid = $("#pid").val();
		var tm1 = $("#tm1").val();
		var tm2 = $("#tm2").val();
		var deposit = $("#deposit").val();
		var roomnum = $("#roomnum").val();
		var allfullfees = $("#allfullfees").val();
		var fullfees = $("#fullfees").val();
		var firstNightFees = $("#firstNightFees").val();
		//var arriveTime = $("#arrviallate").val();
		var arrvialearlyElement = document.getElementById('arrvialearly');
		var arriveTime = $("input[name='arriveTime']:checked").val();
		var arrtime = arriveTime.split(':');
		
		if(rules.length >0) {  //担保信息是否为空
			var garantext = '';
			var bookingDate = new Date();
			var sArray = tm1.split('-');
			var checkInDate = new Date(parseInt(sArray[0],10),(parseInt(sArray[1],10)-1),parseInt(sArray[2],10));
			var fullyFees = fullfees;
			// 房间数量
			var roomCount = roomnum;
			var arriveTime2 = new Date(2011, 5, 20, parseInt(arrtime[0],10), parseInt(arrtime[1],10), 0);
			
			var result = checkGaranteeableByArray(hotelGaranteeRule, bookingDate, checkInDate, arriveTime2, parseInt(roomCount), parseInt(firstNightFees), parseInt(fullyFees));
			if (result) {
				garantext = garanteeableMessage;
				setgarantee(garantext,deposit);
			} else {
				$("#ischeckguarantee").val('no');
				$("#checkguarantee").hide();	
			}
		}
	});
	//根据参数变化设置价格
	function setprice(roomnum,fullfees) {
		var arrfee = param_integral.split('|');
		if(arrfee[0]==1) {
			if(arrfee[1]==1) {
				$("#integral").text(Math.round((fullfees * roomnum)*(arrfee[3])/100)+"分");
			} else {
				$("#integral").text(arrfee[2]+"分");
			}
		}
		$("#myfees").text((fullfees * roomnum) + "元");
		$("#text_allfees").text((fullfees * roomnum) + "元");
		$("#allfullfees").val(fullfees * roomnum);
		$("#text_roomnum").text(roomnum);
		var input = '';
		for(i=1;i<=roomnum;i++) {
			input += "<input id=\"guestname"+i+"\" type=\"text\" name=\"guestname\" value=\"\" class=\"infoInput\" />";
			if(i != roomnum) { input += "　";}
		}
		$("#guest").html(input);
	}
	function setgarantee(data,deposit) {
		$("#ischeckguarantee").val('yes');
		$("#ruleguarantee").html(data);
		$("#dbdesc").html(data);
		$("#checkguarantee").show();
		var allfullfees = $("#allfullfees").val();
		var firstNightFees = $("#firstNightFees").val();
		if(deposit==1) {
			$("#qeetext").text(firstNightFees);
		} else if(deposit==2) {
			$("#qeetext").text(allfullfees);
		}
	}
	function isEmail(str) {
		var myReg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
		if (myReg.test(str)) return true;
		return false;
	}
	$('#ordersubmit').click(function(){
		$c = true;
		$('input[name="guestname"]').each(function(){
			if($(this).val().length<2){
				$c = false;
				$(this).focus();
			}
		})
		if(!$c) {
			alert("请输入客人姓名，每个房间填写一人姓名即可");	
			return false;
		}
		if ($('#contactname').val().length<2){
			alert("请输入联系人姓名");
			$('#contactname').focus();
			return false;
		}
		if (!new RegExp(/^1\d{10}$/).test($('#contactmobile').val())){
			alert("请输入正确的手机号码");
			$("#contactmobile").focus();
			return false;
		}
		if($('#contactemail').val() != ''){
			if(!isEmail($('#contactemail').val())){
				alert("请填写正确的邮件地址");
				$('#contactemail').focus();
				return false;
			}
		}
		
		var time1= document.getElementById("arrvialearly").checked;
		var time2= document.getElementById("arrviallate").checked;
		if(!time1 && !time2) {
			alert("请选择到酒店时间");
			return false;	
		}
		var ischeckguarantee = $("#ischeckguarantee").val();
		if(ischeckguarantee == "yes"){
			if ($('#pu_cardno').val().length<10){
			   alert("请输入完整的信用卡卡号！");
			   $('#pu_cardno').focus();
			   return false;
			}
			
			var dy = parseInt(document.getElementById("pu_year").value);
			var dm = parseInt(document.getElementById("pu_month").value);
			var n = new Date();
			var ny = n.getYear()*1;
			var nm = n.getMonth()+1;
			if(ny>dy){
				alert("您选择的信用卡有效期年份已过，请重新选择！");
				$("#pu_year").focus();
				return false;
			}else{
				if(ny==dy){
					if(nm>dm){
						alert("您选择的信用卡有效期已过，请重新选择！");
						$("#pu_year").focus();
						return false;
					}
				}
			}
			if ($('#pu_code').val().length!=3){
				alert("请输入正确的CVV2编码！");
				$('#pu_code').focus();
				return false;
			}		
			if ($('#pu_name').val().length<2){
				alert("请输入完整的持卡人姓名！");
				$('#pu_name').focus();
				return false;
			}
			  
			if ($('#pu_idno').val().length<4){
				alert("请输入完整的证件号码！");
				$('#pu_idno').focus();
				return false;
			}		 
		}
		var offset = $('#ordersubmit').offset();
		$('#searchtips').css({ left:offset.left, top:offset.top-82});
		$('#searchtips').show();
		$('#ordersubmit').attr('disabled',true);
		var hid = $("#hid").val();
		var rid = $("#rid").val();
		var pid = $("#pid").val();
		var tm1 = $("#tm1").val();
		var tm2 = $("#tm2").val();
		//var arriveTime = $("input[name='arriveTime']:checked").val();
		var arrvialearly = document.getElementById("arrvialearly");
		var arrviallate  = document.getElementById("arrviallate");
		var arrivalEarlyTime = arrivalLateTime = "";
		if(arrvialearly.checked) {
			arrivalEarlyTime = (parseInt(arrvialearly.value,10)-3)+":00";
			arrivalLateTime  = arrvialearly.value;
		}
		if(arrviallate.checked) {
			arrivalEarlyTime = arrvialearly.value;
			arrivalLateTime  = "06:00";
		}
		var note = $("#note").val();
		var roomnum = $("#roomnum").val();
		var reg = "no";
		var chreg = document.getElementById("reg").checked;
		if(chreg) {
			reg = "yes";
		}
		var firstNightFees = $("#firstNightFees").val();
		var allfullfees = $("#allfullfees").val();
		var guestname = '';
		$('input[name="guestname"]').each(function(){
			guestname += $(this).val()+",";
		})
		guestname = guestname.substr(0,(guestname.length-1));
		var contactname = $("#contactname").val();
		var contactmobile = $("#contactmobile").val();
		var contactemail = $("#contactemail").val();
		var ischeckguarantee = $("#ischeckguarantee").val();
		var pu_name = $("#pu_name").val();
		var pu_cardno = $("#pu_cardno").val();
		var pu_year = document.getElementById("pu_year").value;
		var pu_month = document.getElementById("pu_month").value;
		var pu_code = $("#pu_code").val();
		var pu_idtype = document.getElementById("pu_idtype").value;
		var pu_idno = $("#pu_idno").val();
		var hotelinfo = $("#hotelinfo").val();

		$.ajax({
			type: "POST",
			url: siteurl+"index.php",
			data: "ac=order&at=hotelordersave&hid="+hid+"&rid="+rid+"&pid="+pid+"&tm1="+tm1+"&tm2="+tm2+"&arrivalEarlyTime="+arrivalEarlyTime+"&arrivalLateTime="+arrivalLateTime+"&note="+note+"&roomnum="+roomnum+"&allfullfees="+allfullfees+"&firstNightFees="+firstNightFees+"&guestname="+guestname+"&contactname="+contactname+"&reg="+reg+"&contactmobile="+contactmobile+"&contactemail="+contactemail+"&ischeckguarantee="+ischeckguarantee+"&pu_name="+pu_name+"&pu_cardno="+pu_cardno+"&pu_year="+pu_year+"&pu_month="+pu_month+"&pu_code="+pu_code+"&pu_idtype="+pu_idtype+"&pu_idno="+pu_idno+"&hotelinfo="+hotelinfo+"&ran="+Math.random(),
			success: function(data){
				try  {
					data = eval("("+data+")");
					$('#searchtips').hide();
					if(data.code == -1 || data.code == -2) {
						$('#ordersubmit').attr('disabled',false);
						alert(data.message);
						//$("#bodytext").hide();
						//$("#failure").show();
						//$("#success").hide();
					} else if(data.code == 1) {
						var re = data.message;
						$("#text_guestname").text(contactname);
						$("#orderid").text(re.HotelOrderId);
						$("#text_orderid").text(re.HotelOrderId);
						$("#text_allfees").html(allfullfees+" 元");
						$("#text_time").text(arrivalLateTime);
						$("#text_roomnum").text(roomnum);
						$("#text_tm1").text(tm1);
						$("#text_tm2").text(tm2);
						$("#bodytext").hide();
						$("#failure").hide();
						$("#success").show();
					}
				}
				catch(exception) {
					$('#searchtips').hide();
					$('#ordersubmit').attr('disabled',false);
					alert(data);
				}
			},
			error:function (xhr, ajaxOptions, thrownError){
				$('#searchtips').hide();
				$('#ordersubmit').attr('disabled',false);
				alert("对不起，订单提交过程中发生了错误，错误代码："+xhr.status+","+"错误信息："+xhr.responseText+"\r\n"+thrownError+",请把该错误信息提交给我们客服，谢谢。");
			}
		});
	});
});

function changedate(){
	if($("#tm1").val()=="") {
		$("#tm1").focus();
		alert("入住日期不能为空");
		return false;
	}
	if($("#tm2").val()=="") {
		$("#tm2").focus();
		alert("离店日期不能为空");
		return false;
	}
	if($("#tm1").val() != '' && $("#tm2").val() != '') {
		if(!checkdate($("#tm1").val()) || !checkdate($("#tm2").val())) {
			if(!checkdate($("#tm1").val())) {
				$("#tm1").focus();	
			} else if(!checkdate($("#tm2").val())) {
				$("#tm2").focus();	
			}
			alert("日期格式错误，请重新选择");
			return false;
		}
		var d = gethasdate($("#tm1").val(),$("#tm2").val());
		if(d >= 0) {
			$("#tm2").focus();
			alert("日期选择错误，入住日期不能大于等于离开日期！");
			return false;
		}
	}
	var tm1 = $("#tm1").val();
	var tm2 = $("#tm2").val();
	var hid = $("#hid").val();
	var rid = $("#rid").val();
	var pid = $("#pid").val();
	var dbtm2 = $("#dbtm2").val();
	var linkurl = $("#linkurl").val();
	linkurl = linkurl.replace("{#hid#}",hid);
	linkurl = linkurl.replace("{#rid#}",rid);
	linkurl = linkurl.replace("{#pid#}",pid);
	linkurl = linkurl.replace("{#tm1#}",tm1);
	linkurl = linkurl.replace("{#tm2#}",tm2);
	window.location.href = linkurl+"#order";
}

$(function(){
   $('#tm1,#tm1tips').click(function(){showCalendar('tm1','tm2','changedate')})
   $('#tm2,#tm2tips').click(function(){showCalendar('tm2','','changedate');})
});

function checkdate(datestr) 
{ 
	var   reg=/\d{4}-\d{1,2}-\d{1,2}/ 
	if(reg.test(datestr)){return   true} 
	else{return   false} 
	var   str=f1.text.value 
	var   c_str=str.splite( "- ") 
	if(c_str[1]> 12){return   false}
	else   if(c_str[2]> 31){return   fale}
}
function gethasdate(startday,endday) {
	var fArray = startday.split('-');
	var sArray = endday.split('-');
	var fDate = new Date(parseInt(fArray[0],10),(parseInt(fArray[1],10)-1),parseInt(fArray[2],10));
	var sDate = new Date(parseInt(sArray[0],10),(parseInt(sArray[1],10)-1),parseInt(sArray[2],10));
	var t = fDate.getTime()-sDate.getTime();
	var days = t/(24 * 3600 * 1000);
	return days;
}